package quantum2.draw;

import quantum2.graph.Graph;
import quantum2.graph.Matrix;
import quantum2.math.Arithmetic;
import quantum2.math.Complex;

import javax.swing.*;
import java.awt.*;

public class MainPanel extends JPanel {

    private final JTextField tfA = new JTextField(10);
    private final JTextField tfB = new JTextField(10);
    private final JTextField tfC = new JTextField(10);
    private final JTextField tfK = new JTextField(10);

    private final JTextField tfLen0 = new JTextField(10);
    private final JTextField tfLen1 = new JTextField(10);
    private final JRadioButton[] rbs;

    public MainPanel() {
        super(new BorderLayout());
        JPanel matrix = new JPanel(new GridBagLayout());
        matrix.add(new JLabel("A:"), new GridBagConstraints(
            0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0
        ));
        matrix.add(tfA, new GridBagConstraints(
            1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0
        ));
        matrix.add(new JLabel("B:"), new GridBagConstraints(
            2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 0, 0, 5), 0, 0
        ));
        matrix.add(tfB, new GridBagConstraints(
            3, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0
        ));
        matrix.add(new JLabel("C:"), new GridBagConstraints(
            2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 0, 0, 5), 0, 0
        ));
        matrix.add(tfC, new GridBagConstraints(
            3, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0
        ));
        matrix.add(new JLabel("K:"), new GridBagConstraints(
            0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0
        ));
        matrix.add(tfK, new GridBagConstraints(
            1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 5), 0, 0
        ));
        JTextField[] tfs = {tfLen0, tfLen1};
        ButtonGroup group = new ButtonGroup();
        rbs = new JRadioButton[tfs.length];
        int row = 3;
        for (int i = 0; i < tfs.length; i++) {
            JTextField tf = tfs[i];
            JRadioButton rb = new JRadioButton("Edge " + (i + 1) + ":");
            group.add(rb);
            rb.setSelected(i == 0);
            rbs[i] = rb;
            int bottom = i == tfs.length - 1 ? 5 : 0;
            matrix.add(rb, new GridBagConstraints(
                0, row, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, bottom, 5), 0, 0
            ));
            matrix.add(tf, new GridBagConstraints(
                1, row, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, bottom, 5), 0, 0
            ));
            row++;
        }
        add(matrix, BorderLayout.WEST);

        tfA.setText("2");
        tfB.setText("1");
        tfC.setText("2");
        tfK.setText("1");
        tfLen0.setText("1");
        tfLen1.setText("sqrt(2)");
    }

    public int getStartEdge() {
        for (int i = 0; i < rbs.length; i++) {
            JRadioButton rb = rbs[i];
            if (rb.isSelected())
                return i;
        }
        return 0;
    }

    public Graph getGraph(Arithmetic a) {
        JSRunner runner = new JSRunner(a);
        Number edge0 = runner.evaluate(tfLen0.getText());
        Number edge1 = runner.evaluate(tfLen1.getText());
        return new Graph(edge0.doubleValue(), edge1.doubleValue());
    }

    private Complex parse(final JTextField tf, String error) {
        try {
            return Complex.parseComplex(tf.getText());
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, error);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    tf.requestFocusInWindow();
                }
            });
            return null;
        }
    }

    public Matrix getMatrix() {
        Complex a = parse(tfA, "Input A");
        Complex b = parse(tfB, "Input B");
        Complex c = parse(tfC, "Input C");
        Complex k = parse(tfK, "Input K");
        if (a == null || b == null || c == null || k == null)
            return null;
        return new Matrix(a, b, c, k);
    }
}
